<template>
  <div class="alphabet">
    <ul>
      <li class="item"
        v-for="item of letters"
        :key="item"
        :ref="item"
        @click="handleLetterClick"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
      >
        {{item}}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'Alphabet',
  props: {
    cities: Object
  },
  computed: {
    letters () {
      const letters = ['#']
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  data () {
    return {
      touchStatus: false
    }
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart (e) {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        let startY = this.$refs['#'][0].offsetTop
        let touchY = e.touches[0].clientY - 85
        let index = Math.floor((touchY - startY) / 20)
        // console.log(startY, touchY, index)
        if (index >= 0 && index < this.letters.length) {
          this.$emit('change', this.letters[index])
        }
      }
    },
    handleTouchEnd (e) {
      this.touchStatus = false
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/variables.styl'
.alphabet {
  display flex
  flex-direction column
  justify-content center
  position absolute
  top 1.68rem
  right 0
  bottom 0
  width 0.4rem
  .item {
    height 0.4rem
    line-height 0.4rem
    text-align center
    color $bgColor
    margin 0
    padding 0
  }
}
</style>
